package com.dmyang.controller;

import com.dmyang.dao.JedisClientSingle;
import com.dmyang.service.UserService;
import com.dmyang.vo.ImgResp;
import com.dmyang.vo.Respose;
import com.dmyang.vo.UserVo;
import com.dmyang.websocket.MyWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.WebSocketSession;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by brss on 2017/7/20.
 */
@Controller
public class MainController {

    @Autowired
    private UserService userService;
    @Autowired
    private JedisClientSingle jedisClientSingle;
//    @Autowired
//    private RedisUtil redisUtil;

    @RequestMapping("index")
    public String index(HttpSession session){
        return "index";
    }
    @RequestMapping("test")
    public String demo(HttpSession session){
        return "test";
    }
    @RequestMapping("/showLogin")
    public String showLogin(){
        return "login";
    }
    @RequestMapping("/registe")
    public String registe(){
        return "registe";
    }

    @RequestMapping("addSession")
    public String addSession(HttpSession session,@RequestParam("name") String name,@RequestParam String primaryKey){
        session.setAttribute("pk",primaryKey);
        session.setAttribute("name",name);
        return "demo";
    }
    @RequestMapping("init")
    public String init(){
        return "init";
    }

    /**
     *
     * @param session
     * @param reqPk 请求用户的主键
     * @return
     */
    @RequestMapping("/onlineusers")
    @ResponseBody
    public synchronized List<UserVo> onlineusers(HttpSession session,@RequestParam String reqPk){
        Map<String, WebSocketSession> map= MyWebSocketHandler.userSocketSessionMap;
        Set<String> set=map.keySet();
        Iterator<String> it = set.iterator();
        Map<String,String> onlineMap = new ConcurrentHashMap<>();
        while(it.hasNext()){
            //当前请求用户的pk
            //String nowUserPk = (String) session.getAttribute("pk");
            String pk = it.next();
            String name = (String) map.get(pk).getAttributes().get("name");
            //把当前访问的人从在线列表中去除
            if (!pk.equals(reqPk))
                onlineMap.put(pk,name);
        }
        Set<String> keyset = onlineMap.keySet();
        List<UserVo> onlineList = new ArrayList();
        for (String key : keyset){
            UserVo userVo = new UserVo();
            userVo.setId(key);
            userVo.setName(onlineMap.get(key));
            userVo.setType("kefu");
            userVo.setAvatar("//res.layui.com/images/fly/avatar/00.jpg");
            onlineList.add(userVo);
        }
        System.out.println();
        return onlineList;
    }

    @RequestMapping("/upload/img")
    @ResponseBody
    public Respose uploadImg(){

        Respose respose = new Respose();
        respose.setCode(0);
        respose.setMsg("正常");
        ImgResp imgResp = new ImgResp();
        imgResp.setSrc("http://ouxglh8gn.bkt.clouddn.com/pic-002.jpg");
        respose.setData(imgResp);
        return respose;
    }
}
